package cn.edu.heuet.bnve.adapter;

import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.xuexiang.xui.widget.button.SmoothCheckBox;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import cn.edu.heuet.bnve.view.CircleRelativeLayout;
import cn.edu.heuet.bnve.R;
import cn.edu.heuet.bnve.databinding.ItemListHomeworkBinding;
import cn.edu.heuet.bnve.databinding.ItemListScheduleBinding;
import cn.edu.heuet.bnve.view.group.GroupRecyclerAdapter;
import cn.edu.heuet.bnve.model.Schedule;
import cn.edu.heuet.bnve.utils.ColorUtil;
import cn.edu.heuet.bnve.utils.SharepreferencesUtilSystemSettings;

public class ScheduleAdapter extends GroupRecyclerAdapter<String, Schedule> {


    private  List<Schedule> allSchedules;

    private boolean isAction;
    private boolean isStrike;



    public ScheduleAdapter(Context context,List<Schedule> schedules) {
        super(context);
//        mLoader = Glide.with(context.getApplicationContext());

        isAction = SharepreferencesUtilSystemSettings.getValue(context,"schedule_action",true);
        isStrike = SharepreferencesUtilSystemSettings.getValue(context,"schedule_complete_strike",true);
        this.allSchedules = schedules;
        if(schedules.size()==0){
            Schedule schedule = new Schedule(-1);
            schedules.add(schedule);
        }
        else{
            schedules.add(new Schedule(2));
        }
        LinkedHashMap<String, List<Schedule>> map = new LinkedHashMap<>();
        List<String> titles = new ArrayList<>();
        map.put("日程 (长按勾选/取消勾选)", schedules);
        titles.add("日程 (长按勾选/取消勾选)");

        resetGroups(map,titles);


    }

    @Override
    protected RecyclerView.ViewHolder onCreateDefaultViewHolder(ViewGroup parent, int type) {
        if (type==0){
            return new ScheduleAdapter.ScheduleViewHolder(mInflater.inflate(R.layout.item_list_schedule, parent, false));
        }
        else if(type==1){
            return new ScheduleAdapter.HomeworkViewHolder(mInflater.inflate(R.layout.item_list_homework, parent, false));
        }
        else if(type==2){
            return new ScheduleAdapter.BottomViewHolder(mInflater.inflate(R.layout.item_list_bottom, parent, false));
        }
        else{
            return new ScheduleAdapter.BlankViewHolder(mInflater.inflate(R.layout.item_list_no_schedule, parent, false));
        }
    }


    @Override
    public int getItemViewType(int position) {
        if (allSchedules.get(position).getType()==0) {
            return 0;
        } else if(allSchedules.get(position).getType()==1){
            return 1;
        }
        else if(allSchedules.get(position).getType()==2){
            return 2;
        }
        else{
            return -1;
        }
    }


    @RequiresApi(api = Build.VERSION_CODES.P)
    @Override
    protected void onBindViewHolder(RecyclerView.ViewHolder holder, Schedule item, int position) {

        if(item.getType()==0) {
            ScheduleAdapter.ScheduleViewHolder h = (ScheduleAdapter.ScheduleViewHolder) holder;
            h.mTextTitle.setText(item.getTitle());
            if (item.getIsAllday()==1){
                h.mTextTime.setText("全天");
            }
            else {
                h.mTextTime.setText(String.format("%02d", item.getStartTimeHour()) + ":" + String.format("%02d", item.getStartTimeMinute()) +
                        "-" + String.format("%02d", item.getEndTimeHour()) + ":" + String.format("%02d", item.getEndTimeMinute()));
            }
            h.circle.setColor(ColorUtil.getBackgroundColor(item.getAlarmColor()));
            if(isAction) {
                ObjectAnimator rotation = ObjectAnimator.ofFloat(h.circle, "rotation", 0f, 360f).setDuration(600);
                rotation.start();
            }
            if(item.getIsComplete()==1){
                h.mCheckBox.setChecked(true);
                if(isStrike) {
                    h.mTextTitle.setTextColor(Color.GRAY);
                    h.mTextTitle.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);  //中划线，会有锯齿
                    h.mTextTitle.getPaint().setAntiAlias(true);                     //消除锯齿
                }
            }
        }
        else if(item.getType()==1)  {
            ScheduleAdapter.HomeworkViewHolder h = (ScheduleAdapter.HomeworkViewHolder) holder;
            h.mTextTitle.setText(item.getTitle());
            h.mTextTime.setText("截止时间: "+String.format("%02d",item.getEndTimeHour())+":"+String.format("%02d",item.getEndTimeMinute()));
            h.circle.setColor(ColorUtil.getBackgroundColor(item.getAlarmColor()));
            if(isAction) {
                ObjectAnimator rotation = ObjectAnimator.ofFloat(h.circle, "rotation", 0f, 360f).setDuration(600);
                rotation.start();
            }
            if(item.getIsComplete()==1){
                h.mCheckBox.setChecked(true);
                if(isStrike) {
                    h.mTextTitle.setTextColor(Color.GRAY);
                    h.mTextTitle.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);  //中划线，会有锯齿
                    h.mTextTitle.getPaint().setAntiAlias(true);                     //消除锯齿
                }
            }
        }
        else {

        }
    }

    private static class ScheduleViewHolder extends RecyclerView.ViewHolder {
        private TextView mTextTitle, mTextTime;
        private CircleRelativeLayout circle;
        private SmoothCheckBox mCheckBox;

        @SuppressLint("ResourceAsColor")
        private ScheduleViewHolder(View itemView) {
            super(itemView);
            ItemListScheduleBinding binding = DataBindingUtil.bind(itemView);
            mTextTitle = binding.txSchedule;
            mTextTime = binding.txScheduleTime;
            mCheckBox = binding.cbComplete;
            circle = binding.circle;

        }

    }

    private static class HomeworkViewHolder extends RecyclerView.ViewHolder {
        private TextView mTextTitle,mTextTime;
        private SmoothCheckBox mCheckBox;
        private CircleRelativeLayout circle;

        @SuppressLint("ResourceAsColor")
        private HomeworkViewHolder(View itemView) {
            super(itemView);
            ItemListHomeworkBinding binding = DataBindingUtil.bind(itemView);
            mTextTitle = binding.txHomework;
            mCheckBox = binding.cbComplete;
            circle = binding.circle;
            mTextTime = binding.txHomeworkDdl;
        }
    }

    private static class BlankViewHolder extends RecyclerView.ViewHolder {
        @SuppressLint("ResourceAsColor")
        private BlankViewHolder(View itemView) {
            super(itemView);
        }
    }

    private static class BottomViewHolder extends RecyclerView.ViewHolder {
        @SuppressLint("ResourceAsColor")
        private BottomViewHolder(View itemView) {
            super(itemView);
        }
    }

    private  List<Schedule> create(int p) {
        List<Schedule> list = new ArrayList<>();
            for(Schedule s: this.allSchedules){
                if (s.getType()==p){
                    list.add(s);
                }
            }
        return list;
    }





}
